<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <!--搜索条件-->
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.status" placeholder="记录类型"
                       size="medium"
            >
                <el-option
                        v-for="item in statusData"
                        :key="item.val"
                        :label="item.name"
                        :value="item.val"
                >
                </el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    size="medium"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss"
            >
            </el-date-picker>
        </div>
        <!--搜索框-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.searchKeyword" @keyup.enter.native="doSearch" placeholder="按收货人或手机号码搜索" size="medium"></el-input>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch" size="medium"></el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                size="small"
                header-align="center"
                style="width: 100%"

        >
            <!--商品图片-->
            <el-table-column
                    prop="coverPic"
                    label="商品图片"
                    header-align="center"
                    align="center">
                <template scope="scope">
                    <p v-if="scope.row.coverPic">
                        <img :src="scope.row.coverPic" style="width: 60px;height: 60px">
                    </p>
                    <p v-else>
                        暂无数据
                    </p>
                </template>
            </el-table-column>
            <!--商品名称-->
            <el-table-column
                    sortable
                    prop="name"
                    label="商品名称"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--商品价格-->
            <el-table-column
                    sortable
                    prop="price"
                    label="商品价格"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--订单状态-->
            <el-table-column
                    sortable
                    prop="status"
                    label="状态"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    <span>{{getStatus(scope.row.status)}}</span>
                </template>
            </el-table-column>
            <!--收货人-->
            <el-table-column
                    label="收货人"
                    prop="receiverName"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--手机号码-->
            <el-table-column
                    sortable
                    prop="receiverPhone"
                    label="手机号码"
                    header-align="center"
                    align="center">
            </el-table-column>
            <!--收货地址-->
            <el-table-column
                    label="收货地址"
                    prop="addr"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--备注-->
            <el-table-column
                    label="备注"
                    prop="remark"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    align="center">
            </el-table-column>
            <!--创建时间-->
            <el-table-column
                    sortable
                    prop="createAt"
                    label="创建时间"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.createAt)}}
                </template>
            </el-table-column>
            <!--操作时间-->
            <el-table-column
                    sortable
                    prop="opAt"
                    label="操作时间"
                    header-align="center"
                    align="center">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>
            <!--操作下拉菜单-->
            <el-table-column
                    label="操作"
                    width="120"
                    align="center">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item divided :command="{type:'cancel',id:scope.row.id,status:scope.row.status,name:scope.row.name,receiverName:scope.row.receiverName}">
                                取消订单
                            </el-dropdown-item>
                            <el-dropdown-item  v-if="scope.row.status=='PAID'" divided :command="{type:'delivery',id:scope.row.id}">
                                发货
                            </el-dropdown-item>
                            <el-dropdown-item  v-if="scope.row.status=='DELIVERY'" divided :command="{type:'finished',id:scope.row.id}">
                                已完成
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <!--分页容器-->
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

    <el-dialog
            title="填写物流信息"
            :visible.sync="deliveryDialogVisible"
            width="40%">
        <el-form :model="formData" ref="deliveryForm" size="small" label-width="80px" :label-position="labelPosition">
            <el-form-item prop="code" label="物流公司">
                <el-input maxlength="100" placeholder="物流公司"
                          v-model="formData.logisticsCompany"
                          auto-complete="off" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="单号">
                <el-input maxlength="100" placeholder="单号"
                          v-model="formData.logisticsNo"
                          auto-complete="off" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="code" label="备注">
                <el-input maxlength="100" placeholder="备注"
                          v-model="formData.remark"
                          auto-complete="off" type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="deliveryDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doDelivery">确 定</el-button>
        </span>
    </el-dialog>

</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            var self = this;
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        var threeMonths = moment().add(-3, 'months').valueOf();
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1)
                            || time.getTime() < threeMonths;
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                labelPosition: "left",
                searchDate: [],
                statusData:[],
                deliveryDialogVisible: false,
                tableData: [],
                pageForm: {
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    status: "",
                    searchDate: ""
                },
                formData: {
                    id: "",
                },
            }
        },
        methods: {
            getStatus:function(val){
                for (let statusDataKey in this.statusData) {
                    if (this.statusData[statusDataKey].val == val) {
                        return this.statusData[statusDataKey].name;
                    }
                }
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            statusLoad: function () {
                var self = this;
                // 分类数据请求接口
                $.post(base + "/platform/user/mall/exchange/statusList/", function (data) {
                    if (data.code == 0) {
                        self.statusData = data.data;
                        self.statusData.unshift({
                            val: '',
                            name: '全部类型'
                        })
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                } else {
                    self.pageForm.searchDate = "";
                }
                $.post(base + "/platform/user/mall/exchange/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                // 取消订单按钮
                if ("cancel" == command.type) {
                    self.$confirm('您确定要取消 【' + command.name + ' '+ command.receiverName +'】的订单？ ', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/user/mall/exchange/cancel/" + command.id, function (data) {
                                    if (data.code == 0) {
                                        self.pageData();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                } else if("delivery"==command.type){
                    this.deliveryDialogVisible = true;
                    this.formData = {
                        id: command.id,
                    }
                } else if("finished" == command.type){
                    $.post(base + "/platform/user/mall/exchange/finished/" + command.id, function (data) {
                        if (data.code == 0) {
                            self.pageData();
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
            },
            doDelivery: function(){
                var self = this;
                $.post(base + "/platform/user/mall/exchange/doDelivery", self.formData, function (data) {
                    if (data.code == 0) {
                        self.deliveryDialogVisible = false;
                        self.pageData();
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            }
        },
        created: function () {
            this.statusLoad();
            this.pageData();
        }
    });
</script>

<!--#
}
#-->